﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessObject;
using Utility;
namespace WebTeamplate
{
    public partial class Admin_SubCategory : BasePage
    {
        #region Fields
        public string paramater
        {
            get
            {
                object o = ViewState["paramater"];
                if (o != null) return (string)o;
                return string.Empty;
            }
            set { ViewState["paramater"] = value; }
        }
        public string urlDetail = "SubCategoryDetail.aspx";
        int count = 0;
        #endregion

        #region ViewState
        public int F_PageIndex
        {
            get
            {
                object o = ViewState["F_PageIndex"];
                if (o != null) return (int)o;
                return 0;
                //a default

            }
            set { ViewState["F_PageIndex"] = value; }

        }
        public int F_PageSize
        {
            get
            {
                object o = ViewState["F_PageSize"];
                if (o != null) return (int)o;
                return 0;
                //a default

            }
            set { ViewState["F_PageSize"] = value; }

        }
        public string F_SortField
        {

            get
            {
                object o = ViewState["F_SortField"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["F_SortField"] = value; }

        }
        public string F_SortExpression
        {

            get
            {
                object o = ViewState["F_SortExpression"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["F_SortExpression"] = value; }

        }
        #endregion

        #region Init,Load
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSearch.ClientID);
            if (!Page.IsPostBack)
            {
                InitData();
                LoadData();
                F_txtName.Focus();
            }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitTextControl();
        }
        #endregion

        #region Methods
        public void InitTextControl()
        {
            btnSearch.Text = Resources.Resource.SearchButtonText;
            btnReset.Text = Resources.Resource.ResetButtonText;
            btnAddNew.Text = Resources.Resource.AddNewButtonText;
            btnDelete.Text = Resources.Resource.DeleteText;
        }
        private void InitData()
        {
            // Pager1.PageSize = CommonFunction.DEFAULT_ADMIN_PAGE_SIZE;
            CommonFunction.InitLanguageToDropList(drlLanguage, false);
            CommonFunction.InitStatusPublishForRadioButtonList(F_rblStatus);
            CommonFunction.LoadCategory(F_drlCategory, string.Empty, false);
            this.F_drlCategory.SelectedValue = CommonFunction.GetValueFromQueryString(this.Page, "F_drlCategory");
            this.F_PageIndex = ConvertValueObject.ParseInt(CommonFunction.GetValueFromQueryString(this.Page, "F_PageIndex"));
            if (F_PageIndex < 1)
                F_PageIndex = 1;
            this.F_PageSize = ConvertValueObject.ParseInt(CommonFunction.GetValueFromQueryString(this.Page, "F_PageSize"));
            if (F_PageSize < 1)
                F_PageSize = CommonFunction.AdminPageSize;
            F_rblStatus.SelectedValue = CommonFunction.GetValueFromQueryString(this.Page, "F_rblStatus");
            F_SortExpression = CommonFunction.GetValueFromQueryString(this.Page, "F_SortExpression");
            if (F_SortExpression == string.Empty)
                F_SortExpression = "ASC";
            F_SortField = CommonFunction.GetValueFromQueryString(this.Page, "F_SortField");
            if (F_SortField == string.Empty)
                this.F_SortField = "[Order]";
        }
        public void LoadData()
        {
            paramater = this.GetPageParams(FilterFieldType.All);
            //paramater = paramater + "F_rblStatus=" + F_rblStatus.SelectedValue;
            hidListId.Value = string.Empty;
            hidListIdSelect.Value = string.Empty;
            SubCategory objCate = new SubCategory();
            objCate.ObjectSortOrder.SortExpression = this.F_SortExpression;
            objCate.ObjectSortOrder.SortField = this.F_SortField;
            objCate.ObjectPaging.PageIndex = this.F_PageIndex;
            objCate.ObjectPaging.PageSize = this.F_PageSize;
            objCate.Condition = " where 1=1 ";
            if (F_drlCategory.SelectedValue != string.Empty)
                objCate.Condition += " and categoryId=" + F_drlCategory.SelectedValue + "";
            //objCate.Condition = "where 1=1  and categoryId='00'";
            if (F_txtName.Text != string.Empty)
                objCate.Condition += " and subCategoryName like N'%" + F_txtName.Text + "%' ";
            if (F_rblStatus.SelectedValue != string.Empty)
                objCate.Condition += " and Published='" + F_rblStatus.SelectedValue + "' ";
            List<SubCategory> lstResult = SubCategoryUtil.SearchSubCategory(objCate, drlLanguage.SelectedValue);
            if (lstResult != null && lstResult.Count > 0)
                count = lstResult.Count;
            rptData.DataSource = lstResult;
            rptData.DataBind();
            pagerTop.SetPaging(this.F_PageIndex, this.F_PageSize, count, objCate.ToltalRow);
            pagerBottom.SetPaging(this.F_PageIndex, this.F_PageSize, count, objCate.ToltalRow);
        }
        private void Delete(string id)
        {
            if (SubCategoryUtil.Delete(ConvertValueObject.ParseInt(id)))
                LoadData();
        }
        public void Publish(string id)
        {

            if (SubCategoryUtil.Publish(ConvertValueObject.ParseInt(id)))
                LoadData();
        }
        private void SortList(string id)
        {
            this.F_SortField = id;
            if (this.F_SortExpression == CommonFunction.SortDownDirection)
                this.F_SortExpression = CommonFunction.SortUpDirection;
            else this.F_SortExpression = CommonFunction.SortDownDirection;
            LoadData();
        }
        private void OrderDown(string id)
        {
            if (SubCategoryUtil.Order(ConvertValueObject.ParseInt(id), CommonFunction.OrderDown))
                LoadData();
        }
        private void OrderUp(string id)
        {

            if (SubCategoryUtil.Order(ConvertValueObject.ParseInt(id), CommonFunction.OrderUp))
                LoadData();
        }
        #endregion

        #region Events,Handler
       
        protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                SubCategory cate = (SubCategory)e.Item.DataItem;
                if (cate == null)
                    return;
                Literal ltr = (Literal)e.Item.FindControl("ltrNo");
                ltr.Text = (e.Item.ItemIndex + 1).ToString();

                //ltr = (Literal)e.Item.FindControl("ltrSubCode");
                string url = urlDetail + "?" + paramater + "&subcategoryId=" + cate.SubCategoryId + "&act=" + CommonFunction.EdityCommand;
                //ltr.Text = "<a href='" + url + "'>" + Server.HtmlEncode(cate.SubCategoryCode) + "</a>";
                ltr = (Literal)e.Item.FindControl("ltrSubName");
                ltr.Text = "<a href='" + url + "'>" + Server.HtmlEncode(cate.SubCategory_TransCollection[0].SubCategoryName) + "</a>";
                ImageButton imgButton = (ImageButton)e.Item.FindControl("btnPublish");
                imgButton.CommandArgument = cate.SubCategoryId.ToString();
                if (cate.Published == CommonFunction.True)
                    imgButton.ImageUrl = CommonFunction.ActiveImage;
                else
                    imgButton.ImageUrl = CommonFunction.DeActiveImage;
                imgButton = (ImageButton)e.Item.FindControl("imgOrderUp");
                if (e.Item.ItemIndex + 1 == count)
                    imgButton.Visible = false;
                else
                {
                    imgButton.ToolTip = Resources.Resource.OrderUpTooltip;
                    imgButton.CommandArgument = cate.SubCategoryId.ToString();
                    imgButton.ImageUrl = CommonFunction.OrderUpImage;
                }
                imgButton = (ImageButton)e.Item.FindControl("imgOrderDown");
                if (e.Item.ItemIndex == 0)
                    imgButton.Visible = false;
                else
                {
                    imgButton.CommandArgument = cate.SubCategoryId.ToString();
                    imgButton.ToolTip = Resources.Resource.OrderDownTooltip;
                    imgButton.ImageUrl = CommonFunction.OrderDownImage;
                    imgButton.Visible = true;
                }
                HyperLink hplEdit = (HyperLink)e.Item.FindControl("hplEdit");
                hplEdit.NavigateUrl = url;
                hplEdit.Text = Resources.Resource.EditText;
                LinkButton lbtnDelete = (LinkButton)e.Item.FindControl("lbtnDelete");
                lbtnDelete.Text = Resources.Resource.DeleteText;
                lbtnDelete.CommandArgument = cate.SubCategoryId.ToString();
                hidListId.Value += cate.SubCategoryId + ";";
            }
        }
        protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "publish":
                    {
                        Publish(id);
                        break;
                    }
                case "delete":
                    {
                        Delete(id);
                        break;
                    }
                case "OrderUp":
                    {
                        OrderUp(id);
                        break;
                    }
                case "OrderDown":
                    {
                        OrderDown(id);
                        break;
                    }
                case "SortOrder":
                    {
                        SortList(id);
                        break;
                    }

            }

        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.F_SortExpression = "ASC";
            this.F_SortField = "[Order]";
            //reset sort order
            this.F_PageIndex = 1;
            LoadData();
        }
        void imgSubCodeSortDown_Click(object sender, ImageClickEventArgs e)
        {
            this.F_SortField = "SubCategoryName";
            this.F_SortExpression = "DESC";
            LoadData();
        }
        void imgSubCodeSortUp_Click(object sender, ImageClickEventArgs e)
        {
            this.F_SortField = "SubCategoryName";
            this.F_SortExpression = "ASC";
            LoadData();
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect(urlDetail + "?" + paramater);
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            // List<SubCategory> lstCate = new List<SubCategory>();
            string[] arr = hidListIdSelect.Value.Split(';');
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i].ToString() != string.Empty)
                {
                    SubCategoryUtil.Delete(ConvertValueObject.ParseInt(arr[i].ToString()));
                }
            }
            //   SubCategoryUtil.Delete(lstCate);
            LoadData();
        }
        protected void pagerTop_PageIndexChanging(object obj, PagerControl.PageIndexChangeEventArgs e)
        {
            this.F_PageIndex = e.newPageIndex;
            LoadData();
        }
        protected void pagerTop_PageSizeChanging(object obj, PagerControl.PageSizeChangeEventArgs e)
        {
            this.F_PageIndex = 1;
            this.F_PageSize = e.newPageSize;
            LoadData();
        }
        protected void pagerBottom_PageIndexChanging(object obj, PagerControl.PageIndexChangeEventArgs e)
        {
            this.F_PageIndex = e.newPageIndex;
            LoadData();
        }
        protected void pagerBottom_PageSizeChanging(object obj, PagerControl.PageSizeChangeEventArgs e)
        {
            this.F_PageIndex = 1;
            this.F_PageSize = e.newPageSize;
            LoadData();
        }
        protected void drlLanguage_SelectedIndexChanged1(object sender, EventArgs e)
        {
            this.F_PageIndex = 1;
            LoadData();
        }
        #endregion
       
    }
}
